package Object_demo;

import java.util.Objects;

public class equals {
    public static void main(String[] args) {
        man n1=new man("李佳辉",18);
        man n2=new man("李佳辉",18);
        //默认比较地址实际开发中比较地址用==
        System.out.println(n1.equals(n2));//false
    }
}
class man{
    private String name;
    private int age;

    public man() {
    }

    public man(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    //重写equals
    //自己写的
//    @Override
//    public boolean equals(Object o) {
//        if (o instanceof man){
//            return this.name.equals(((man) o).name)&&this.age==((man) o).age;
//        }
//        return false;
//    }
    //默认写的
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        //第二个比较类型 man != 比较者类型
        if (o == null || getClass() != o.getClass()) return false;
        man man = (man) o;
        return age == man.age && Objects.equals(name, man.name);
    }
}
